package com.cyan.teamquestionnaire.mappers;

import com.cyan.teamquestionnaire.entity.Group;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface GroupMapper {

    // 新建群组
    @Insert("insert into `groups`(gid, group_name, user_name, tenant_name) values(#{gId}, #{groupName}, #{userName}, #{tenantName})")
    void create(Group group);

    // 删除群组,同时在群组里面的用户信息也删除(group_user)
    @Delete("delete from `groups` where gid = #{gId}")
    void delete(String gId);

    @Delete("delete from group_user where gid = #{gId}")
    void deleteGroupUser(String gId);


    // 我觉得只能修改组名，其他信息修改不了
    @Update("update `groups` set group_name = #{groupName} where gid = #{gId}")
    void modify(Group group);

    // 将用户加入到群组，操作的是group_user表
    @Insert("insert into group_user(gid, user_name) values(#{gId}, #{userName})")
    void addUserToGroup(@Param("userName") String userName, @Param("gId") String gId);

    // 在指定群组中删除用户
    @Delete("delete from group_user where gid = #{gId} and user_name = #{userName}")
    void deleteUserFromGroup(@Param("userName") String userName, @Param("gId") String gId);

    // 查看群组多少个成员
    @Select("select user_name as cnt from group_user where gId = #{gId}")
    int userCnt(String gId);

    // 通过gId查询群组相关信息
    @Select("select * from `groups` where gid = #{gId}")
    Group selectGroupByGId(String gId);

    // 查看某个成员是否在指定群组中
    @Select("select user_name from group_user where gid = #{gId} and user_name = #{name}")
    String checkInGroup(String name, String gId);

    //根据创建的用户名返回组的list
    @Results({
            @Result(column = "group_name", property = "groupName"),
            @Result(column = "user_name", property = "userName"),
            @Result(column = "tenant_name", property = "tenantName"),
    })
    @Select("select * from `groups` where user_name = #{name} and group_name like #{groupName}")
    List<Group> getGroupList(@Param("name")String name, @Param("groupName")String groupName);

    //根据创建的用户名返回组的list————分页模糊查询
    @Results({
            @Result(column = "group_name", property = "groupName"),
            @Result(column = "user_name", property = "userName"),
            @Result(column = "tenant_name", property = "tenantName"),
    })
    @Select("select * from `groups` where user_name = #{name} and group_name like #{query} ")
    List<Group> getGroupListFuzzy(@Param("name")String name,@Param("query")String query);


    // 返回群组成员
    @Select("select user_name from group_user where gId = #{gId}")
    List<String> usersInGroup(String gId);

    //返回qid群组里面的群组成员——模糊查询name
    @Select("select user_name from group_user where gId = #{gId} and user_name like #{query} ")
    List<String> usersInGroupFuzzy(@Param("gId")String gId,@Param("query")String query);

    //返回这个租户管理下的用户还没加入群组的用户
    //要想群组加入用户，首先要选择出来这个租户的所有用户，然后选择出来所有这个群组里面的用户，两者做差集
    @Select("select user_name from tenant_user where tenant_name = #{tenantName} and user_name not in (select user_name from group_user where gid = #{gId}) and user_name like #{query}")
    List<String> getRestUser(@Param("gId")String gId,@Param("tenantName")String tenantName,@Param("query")String query);

    // 查询租户下所有群组数
    @Select("select count(gid) from `groups` where tenant_name = #{name}")
    int countGroupInTenant(String name);

    //返回该租户下的所有群组list，分页模糊
    @Results({
            @Result(column = "group_name", property = "groupName"),
            @Result(column = "user_name", property = "userName"),
            @Result(column = "tenant_name", property = "tenantName"),
    })
    @Select("select * from `groups` where tenant_name = #{name} and group_name like #{query} ")
    List<Group> GroupInTenantFuzzy(@Param("name")String name,@Param("query")String query);
}
